package inet.app.view.fragment;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.connect.JsonParser;
import inet.app.controller.database.DataBaseHandler;
import inet.app.controller.parser.Parser_Tk;
import inet.app.controller.request.Request;
import inet.app.model.object.SubTk_Item;
import inet.app.model.object.Tk_Item;
import inet.app.model.util.ProgressDialog;
import inet.app.model.util.Util;
import inet.app.view.activity.MyApp;

import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentTKN extends BaseFragmentTK implements OnClickListener,
		OnKeyListener {
	private String nhamang = "", cuoc = " (Cước 1000đ)", matinh = "", name,
			json, numberdate;
	private int weekofyear, dayofmonth;
	private Button btXem;
	private WebView webView;
	boolean isSendSms = false;
	private DataBaseHandler db;
	private EditText etcapso;
	private String capso = "null";
	private String[] list_numberdate = new String[] { "10", "20", "30" };
	private Spinner spnumberdate;
	private TextView tvSpDate;
	private SubTk_Item item_sub;
	private ArrayList<Tk_Item> arraylist = new ArrayList<Tk_Item>();
	private Parser_Tk parser = new Parser_Tk();
	private ProgressDialog pdia;
	private Request request = new Request();

	public static Fragment newInstance(Context context) {
		FragmentTKN f = new FragmentTKN();
		return f;
	}

	public static Fragment newInstance(Context context, String position) {
		FragmentTKN f = new FragmentTKN();
		Bundle args = new Bundle();
		args.putString("pos", position);
		f.setArguments(args);
		return f;
	}

	public static Fragment newInstance(Context context, String position,
			String json, String code, String name) {
		FragmentTKN f = new FragmentTKN();
		Bundle args = new Bundle();
		args.putString("pos", position);
		args.putString("json", json);
		args.putString("code", code);
		args.putString("name", name);
		f.setArguments(args);
		return f;
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_tkn,
				null);
		nhamang = Util.getSharePreferences(getActivity());
		pdia = new ProgressDialog(getActivity());
		if (nhamang.contains("VIETTEL")) {
			cuoc = " (Cước 5000đ)";
		} else {
			cuoc = " (Cước 2000đ)";
		}
		// Get Position
		if (getArguments() != null) {
			System.out.println("CHAY VAO 1");
			name = getArguments().getString("name");
			matinh = getArguments().getString("code");
			json = getArguments().getString("json");
		} else {
			System.out.println("cHAY VAO 2");
		}

		Calendar c = Calendar.getInstance();
		weekofyear = c.get(Calendar.WEEK_OF_YEAR);
		dayofmonth = c.get(Calendar.DAY_OF_MONTH);
		btXem = (Button) root.findViewById(R.id.btXem);

		spnumberdate = (Spinner) root.findViewById(R.id.spnumberdate);
		etcapso = (EditText) root.findViewById(R.id.etcapso);
		webView = (WebView) root.findViewById(R.id.webView);
		tvSpDate = (TextView) root.findViewById(R.id.tvSpDate);
		tvSpDate.setOnClickListener(this);

		if (etcapso.getText().toString().equals("") == true) {
			capso = "null";
		} else {
			capso = etcapso.getText().toString();
		}

		etcapso.setOnKeyListener(this);
		// Setup webview
		webView.setWebViewClient(new MyWebViewClient());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setPluginsEnabled(true);
		webView.getSettings().setSupportMultipleWindows(true);
		webView.getSettings().setSupportZoom(false);
		webView.setVerticalScrollBarEnabled(true);
		webView.setHorizontalScrollBarEnabled(false);

		// Setonclick
		btXem.setOnClickListener(this);
		AddSpinnerDate();

		sendrequest("XSTD", "10", "null");

		return root;
	}

	private void AddSpinnerDate() {
		// spiner numberdate
		ArrayAdapter<String> arrayadapter_numberdate = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item,
				list_numberdate);
		arrayadapter_numberdate
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnumberdate.setAdapter(arrayadapter_numberdate);
		spnumberdate
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> arg0, View v,
							final int pos, long id) {
						spnumberdate.setSelection(pos);
						tvSpDate.setText(" " + list_numberdate[pos] + " Ngày");
						switch (pos) {
						case 0:
							numberdate = "10";
							break;
						case 1:
							numberdate = "20";
							break;
						case 2:
							numberdate = "30";
							break;
						}
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				});
	}

	private void sendrequest(String code, String numberdate, String couple) {
		Bundle rb = getActivity().getIntent().getExtras();
		System.out.println("Couple là ============== >>" + couple);
		if (couple.equals("null")) {
			arraylist = parser.getListTK(rb.getString("json"));
		} else {
			arraylist = parser.getList_CsTK(rb.getString("json"));
		}
		if (arraylist != null) {
			pdia.dismiss();
			Load_data_webview(arraylist);
		} else {
			pdia.dismiss();
			webView.clearCache(true);
			webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
			Util.showDialog(1, getActivity());
		}
	}

	private void Load_data_webview(ArrayList<Tk_Item> arraylist) {
		// TODO Auto-generated method stub
		// Clear the cache
		webView.clearCache(true);
		String[] most = {}, least = {}, result = {};
		String htmlThongke = "", htmlMost = "", htmlLeast = "", item = "", htmlTKcs = "", htmlfooter = "";
		most = arraylist.get(0).getMost().split(",");
		least = arraylist.get(0).getLeast().split(",");
		
		String buttonSms = "";
		
		if (!arraylist.get(0).getTkcs().equals("")) {

			if (arraylist.get(0).getTkcs().length() < 27) {
				if (!isFree) {
					buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
							+ "Click Nhận Thống Kê Cặp Số "
							+ name
							+ cuoc
							+ "\" onclick=\"window.location.href='"
							+ matinh
							+ "'\" >";
				} else {
					buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
							+ "Kết Quả Thống Kê Cặp Số "
							+ name
							+ "\" >";
				}
				
				htmlTKcs += "<div  style=\"cursor: pointer; margin:3px 0 0 0; background-color:#323232;width: 100%\">"
						+ buttonSms
						+ "</div>"
						+ "<div style=\"margin:3px 0 0 0; background-color:#e0e0e0; height:80px; text-align:center;\">"
						+ "<span style=\"background-color:#e0e0e0; color:red; text-align:center; position:relative; top:30px\" class=\"fontsize12\">"
						+ "Không có kết quả thống kê cặp số này ! </span>"
						+ "</div>";
			} else {
				if (!isFree) {
					buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
							+ "Click Nhận Thống Kê Cặp Số "
							+ name
							+ cuoc
							+ "\" onclick=\"window.location.href='"
							+ arraylist.get(0).getCode()
							+ "'\" >";
				} else {
					buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
							+ "Kết Quả Thống Kê Cặp Số "
							+ arraylist.get(0).getName()
							+ "\" >";
				}
				
				result = arraylist.get(0).getTkcs().split(",");
				htmlTKcs += "<div>"
						+ "<div  style=\"cursor: pointer; margin:3px 0 0 0; background-color:#323232;width: 100%\">"
						+ buttonSms
						+ "</div>"
						+

						"<div style=\"margin:0 0 0 0; background-color:#e0e0e0; height:10px; text-align:center;\">"
						+ "<span style=\"background-color:#e0e0e0; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
						+ "</span>"
						+ "</div>"
						+

						"<div>"
						+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
						+ "<tr>"
						+

						"<td width=\"100%\" valign=\"top\" colspan=\"12\">"
						+

						"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">";

				for (int j = 0; j < 8; j++) {
					if (result[j].split(":")[0].equals("0")
							&& result[j].split(":").length > 1) {
						htmlTKcs += "<tr bgcolor=\"#e0e0e0\" >"
								+ "<td width=\"50%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >Đặc biệt "
								+ "</td>"
								+ "<td width=\"50%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
								+ result[j].split(":")[1] + " lần" + "</td>"
								+ "</tr>";
					} else if (result[j].split(":").length > 1) {
						htmlTKcs += "<tr bgcolor=\"#e0e0e0\" >"
								+ "<td width=\"50%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >Giải "
								+ result[j].split(":")[0]
								+ "</td>"
								+ "<td width=\"50%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
								+ result[j].split(":")[1] + " lần" + "</td>"
								+ "</tr>";
					}
				}

				htmlTKcs += "</table>"
						+ "</td>"
						+

						"</tr>"
						+ "</table>"
						+ "</div>"
						+

						"<div style=\"margin:0 0 0 0; background-color:#EEEEEE; height:25px; text-align:center;\">"
						+ "<span style=\"background-color:#EEEEEE; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
						+ "</span>" + "</div>" +

						"</div>";
			}
		} else {
			if (!isFree) {
				buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
						+ "Click Nhận Thống Kê Cặp Số "
						+ name
						+ cuoc
						+ "\" onclick=\"window.location.href='"
						+ arraylist.get(0).getCode()
						+ "'\" >";
			} else {
				buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
						+ "Kết Quả Thống Kê Cặp Số "
						+ arraylist.get(0).getName()
						+ "\" >";
			}
			htmlTKcs += "<div  style=\"cursor: pointer; margin:3px 0 0 0; background-color:#323232;width: 100%\">"
					+ buttonSms
					+ "</div>"
					+ "<div style=\"margin:3px 0 0 0; background-color:#e0e0e0; height:80px; text-align:center;\">"
					+ "<span style=\"background-color:#e0e0e0; color:black; text-align:center; position:relative; top:30px\" class=\"fontsize12\">"
					+ "Hãy nhập cặp số cần Thống Kê </span>" + "</div>";
		}

		htmlMost += "<div>"
				+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:30px; text-align:center;\">"
				+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "10 Cặp Số Về Nhiều Nhất "
				+ "</span>"
				+ "</div>"
				+

				"<div style=\"margin:0 0 0 0; background-color:#EEEEEE; height:10px; text-align:center;\">"
				+ "<span style=\"background-color:#EEEEEE; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "</span>"
				+ "</div>"
				+

				"<div>"
				+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
				+ "<tr>"
				+

				"<td width=\"47%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\"  width=\"25\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				// +
				// "<img src=\"file:///android_res/drawable/img_tk_g0.png\" width=\"25\" height=\"25\"/>"
				+ most[0].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[0].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"   background=\"file:///android_res/drawable/icon_giai_ghi.png\" align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[1].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[1].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[2].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[2].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[3].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[3].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[4].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[4].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				
				+ "</table>"
				+ "</td>"
				+ "<td width=\"6%\" valign=\"top\" >"
				+ "<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
				+ "</table>"
				+ "</td>"
				+

				"<td width=\"47%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[5].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[5].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[6].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[6].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[7].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[7].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[8].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[8].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[9].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ most[9].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "</table>"
				+

				"</td>"
				+

				"</tr>"
				+ "</table>"
				+ "</div>"
				+

				"<div style=\"margin:0 0 0 0; background-color:#EEEEEE; height:25px; text-align:center;\">"
				+ "<span style=\"background-color:#EEEEEE; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "</span>" + "</div>" +

				"</div>";

		htmlLeast += "<div>"
				+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:30px; text-align:center;\">"
				+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "10 Cặp Số Về Ít Nhất"
				+ "</span>"
				+ "</div>"
				+ "<div style=\"margin:0 0 0 0; background-color:#EEEEEE; height:10px; text-align:center;\">"
				+ "<span style=\"background-color:#EEEEEE; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "</span>"
				+ "</div>"
				+

				"<div>"
				+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
				+ "<tr>"
				+

				"<td width=\"47%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\"  class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[0].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[0].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[1].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[1].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[2].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[2].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[3].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[3].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[4].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[4].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+

				"</table>"
				+

				"</td>"
				+

				"<td width=\"6%\" valign=\"top\" >"
				+

				"<table width=\"100%\"  border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
				+ "</table>"
				+

				"</td>"
				+

				"<td width=\"47%\" valign=\"top\" colspan=\"12\">"
				+

				"<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
				+

				"<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[5].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[5].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[6].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[6].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[6].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[7].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_nau.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[8].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[8].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"17%\" height=\"25\"  align=\"center\" background=\"file:///android_res/drawable/icon_giai_ghi.png\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[9].split(":")[0]
				+ "</td>"
				+ "<td width=\"83%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ least[9].split(":")[1]
				+ " lần"
				+ "</td>"
				+ "</tr>"
				+ "</table>"
				+

				"</td>"
				+

				"</tr>"
				+ "</table>"
				+ "</div>"
				+

				"<div style=\"margin:0 0 0 0; background-color:#EEEEEE; height:25px; text-align:center;\">"
				+ "<span style=\"background-color:#EEEEEE; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
				+ "</span>" + "</div>" +

				"</div>";
		htmlfooter = "<div style=\"margin:0 0 0 0; background-color:#323232; height:35px; text-align:center;\">"
				+ "<span style=\"background-color:#323232; text-align:center; color:#ffffff; font-size:13px; position:relative; top:10px\" class=\"fontsize12\">"
				+ "<strong>"
				+ "Chơi xổ số kiến thiết ích nước lợi nhà !"
				+ "</strong></span>" + "</div>";
		webView.loadDataWithBaseURL("file:///android_asset/", XoSoConstants.css+htmlTKcs + htmlMost + htmlLeast
				+ htmlfooter+"</body></html>", "text/html", "utf-8", null);

	}

	@SuppressLint("NewApi")
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			// SQLITE DB
			db = new DataBaseHandler(getActivity());
			System.out.println("MA TỈNH == " + url.substring(2, url.length()));
			item_sub = db.getSubTk(matinh);
			System.out.println("DAY OF MONTH  " + dayofmonth);
			if (item_sub != null) {
				System.out
						.println("DỮ LIEU LAY DC " + item_sub.getWeekofyear());
				System.out.println("RUNG 1");
				if (dayofmonth == Integer.parseInt(item_sub.getWeekofyear())) {
					isSendSms = false;
					System.out.println("RUNG 2");
				} else {
					System.out.println("ELLELE");
					db.updateSubTk(item_sub);
					isSendSms = true;
				}
			} else {
				System.out.println("RUNG 3");
				System.out.println("ELLLDLLD22222");
				item_sub = new SubTk_Item(matinh, Integer.toString(dayofmonth));
				db.insertSubTk(item_sub);
				isSendSms = true;
			}

			if (isSendSms) {
				if (nhamang.contains("VIETTEL")) {
					if (url.contains("XSTD")) {
						Util.sendSMS("CAU MB", "8585", getActivity());
					} else {
						Util.sendSMS("CAU " + url.substring(2, url.length()),
								"8585", getActivity());
					}
					Util.SharedPreferencesSub(Integer.toString(dayofmonth),
							matinh, getActivity());
				} else {
					if (url.contains("XSTD")) {
						Util.sendSMS("DK TK MB", "8985", getActivity());
						Util.SharedPreferencesSub(Integer.toString(dayofmonth),
								"submb", getActivity());
					} else {
						if ((url.contains("XSBDH") || url.contains("XSDLK")
								|| url.contains("XSDNG")
								|| url.contains("XSDNO")
								|| url.contains("XSGL") || url.contains("XSKH")
								|| url.contains("XSKT") || url.contains("XSNT")
								|| url.contains("XSPY") || url.contains("XSQB")
								|| url.contains("XSQNI")
								|| url.contains("XSQNM")
								|| url.contains("XSQT") || url
									.contains("XSTTH"))) {
							Util.sendSMS("DK TK MT", "8985", getActivity());
							Util.SharedPreferencesSub("1", "subxstkmt",
									getActivity());
						} else {
							Util.sendSMS("DK TK MN", "8985", getActivity());
							Util.SharedPreferencesSub("1", "subxstkmn",
									getActivity());
						}
					}
				}
				Toast.makeText(getActivity(), "Đăng ký thành công!",
						Toast.LENGTH_LONG).show();
			} else {
				if (nhamang.contains("VIETTEL")) {
					if (Util.getdataSms(getActivity(), " THONG KE")
							&& !Util.getSharePreferencesSub(getActivity(),
									matinh).equals("1")) {
						if (url.contains("XSTD")) {
							Util.sendSMS("CAU MB", "8585", getActivity());
						} else {
							Util.sendSMS(
									"CAU " + url.substring(2, url.length()),
									"8585", getActivity());
						}
						Util.SharedPreferencesSub(Integer.toString(dayofmonth),
								matinh, getActivity());
						Toast.makeText(getActivity(), "Đăng ký thành công!",
								Toast.LENGTH_LONG).show();
					}
				} else {
					if (Util.getdataSms(getActivity(), " THONG KE")) {
						if (url.contains("XSTD")) {
							if (!Util.getSharePreferencesSub(getActivity(),
									"submb").equals("1")) {
								Util.sendSMS("DK TK MB", "8985", getActivity());
								Util.SharedPreferencesSub(
										Integer.toString(dayofmonth), "submb",
										getActivity());
								Toast.makeText(getActivity(),
										"Đăng ký thành công!",
										Toast.LENGTH_LONG).show();
							}
						} else {
							if ((url.contains("XSBDH") || url.contains("XSDLK")
									|| url.contains("XSDNG")
									|| url.contains("XSDNO")
									|| url.contains("XSGL")
									|| url.contains("XSKH")
									|| url.contains("XSKT")
									|| url.contains("XSNT")
									|| url.contains("XSPY")
									|| url.contains("XSQB")
									|| url.contains("XSQNI")
									|| url.contains("XSQNM")
									|| url.contains("XSQT") || url
										.contains("XSTTH"))) {
								if (!Util.getSharePreferencesSub(getActivity(),
										"subxstkmt").equals("1")) {
									Util.sendSMS("DK TK MT", "8985",
											getActivity());
									Util.SharedPreferencesSub("1", "subxstkmt",
											getActivity());
									Toast.makeText(getActivity(),
											"Đăng ký thành công!",
											Toast.LENGTH_LONG).show();
								}
							} else {
								if (!Util.getSharePreferencesSub(getActivity(),
										"subxstkmn").equals("1")) {
									Util.sendSMS("DK TK MN", "8985",
											getActivity());
									Util.SharedPreferencesSub("1", "subxstkmn",
											getActivity());
									Toast.makeText(getActivity(),
											"Đăng ký thành công!",
											Toast.LENGTH_LONG).show();
								}
							}
						}
					}
				}
			}
			return true;
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btXem:
			// sendrequest(matinh, numberdate, capso);
			matinh = ((MyApp) mApplication).getCode();
			pdia.show();
			final Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what == 0) {
						String json = (String) msg.obj;
						pdia.dismiss();
						arraylist = parser.getList_CsTK(json);
						if (arraylist != null) {
							pdia.dismiss();
							Load_data_webview(arraylist);
						} else {
							pdia.dismiss();
							webView.clearCache(true);
							webView.loadDataWithBaseURL(null, "", "text/html",
									"utf-8", null);
							Util.showDialog(6, getActivity());
						}
					} else if (msg.what == 1) {
						pdia.dismiss();
						Util.showDialog(1, getActivity());
					}
				}

			};
			Thread thread = new Thread() {
				public void run() {
					JsonParser jParser = new JsonParser();
					String jobject = jParser.getURL(request.gettk(matinh,
							numberdate, etcapso.getText().toString()));
					if (jobject != null) {
						Message msg = handler.obtainMessage(0, jobject);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(1);
						handler.sendMessage(msg);
					}

				}
			};

			thread.start();
			break;
		case R.id.tvSpDate:
			spnumberdate.performClick();
			break;
		default:
			break;
		}
	}

	public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
		// TODO Auto-generated method stub
		return false;
	}
}
